package com.manor.main.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.manor.common.utils.DpUtil;
import com.manor.common.utils.StringUtil;
import com.manor.main.R;
import com.manor.main.activity.UserCenterActivity;
import com.manor.main.bean.CommentsAndLikesBean;

import java.util.List;

public class CommentsAndLikesAdapter extends BaseQuickAdapter<CommentsAndLikesBean, BaseViewHolder> {

    //设置图片圆角角度
    RoundedCorners roundedCorners = new RoundedCorners(8);
    //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
    // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
    RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);


    public CommentsAndLikesAdapter(int layoutResId, @Nullable List<CommentsAndLikesBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, CommentsAndLikesBean item) {
        ImageView ivAvatarThumb = helper.getView(R.id.iv_avatar_thumb);
        ImageView ivImgUrl = helper.getView(R.id.iv_imgurl);
        TextView tvRightContent = helper.getView(R.id.tv_right_content);
        helper.setText(R.id.tv_title, item.getUser_nickname());
        TextView tv_content = helper.getView(R.id.tv_content);
        TextView tv_addtime = helper.getView(R.id.tv_addtime);
        if(item.getIs_status().equals("1")){//1 点赞 2评论
            tv_addtime.setText(item.getContent()+"   "+item.getAddtime());
            tv_content.setVisibility(View.GONE);
//            tv_addtime.setVisibility(View.GONE);
//            tv_content.setTextColor(Color.parseColor("#80ffffff"));
        }else {
            tv_addtime.setText(item.getContent());
            tv_content.setVisibility(View.VISIBLE);
            tv_content.setText(item.getContent());
            tv_addtime.setText("回复了你的评论  "+item.getAddtime());

        }

        ivAvatarThumb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserCenterActivity.Companion.forward(getContext(),
                       item.getP_uid());
            }
        });

        /**    Glide.with(context).load(if (ishttp(item)) item else "http://online.shand2.com/$item")
         * 内容设置
         */
        if (StringUtil.isNotEmpty(item.getImgurl())) {
            Glide.with(getContext())
                    .load(ishttp(item.getImgurl())? item.getImgurl() : "http://online.shand2.com/"+item.getImgurl())
                    .apply(options)
                    .centerCrop()
                    .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(DpUtil.dp2px(10))))
                    .into(ivImgUrl);
            tvRightContent.setVisibility(View.GONE);
            ivImgUrl.setVisibility(View.VISIBLE);
        } else {
            tvRightContent.setVisibility(View.VISIBLE);
            ivImgUrl.setVisibility(View.GONE);
            tvRightContent.setText(item.getTitle());
        }
        /**
         * 头像设置
         */
        if (StringUtil.isNotEmpty(item.getAvatar_thumb())) {
            Glide.with(getContext())
                    .load(item.getAvatar_thumb())
                    .error(R.mipmap.defaulthead)
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(ivAvatarThumb);
        }
    }
    private Boolean ishttp(String str) {
        String msg = str.substring(0, 4);
         if (msg.equals("http")) {
             return  true;
        } else {
             return  false;
        }
    }
}
